﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NuGet.ProjectModel;
using Quartz.NET.Web.Attr;
using Quartz.NET.Web.Extensions;
using Quartz.NET.Web.Models;
using Quartz.NET.Web.Utility;
using Quartz.Spi;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Quartz.NET.Web.Controllers
{

    public class TaskController : Controller
    {
        private readonly ISchedulerFactory _schedulerFactory;
        private readonly IJobFactory _jobFactory;
        private readonly IFreeSql freeSql;
        public TaskController(ISchedulerFactory schedulerFactory, IJobFactory jobFactory, IFreeSql _fsql)
        {
            this._jobFactory = jobFactory;
            this._schedulerFactory = schedulerFactory;
            freeSql = _fsql;
        }

        public IActionResult Index()
        {
            return View("~/Views/Task/Index.cshtml");
        }

        /// <summary>
        /// 获取所有的作业
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetJobs(string search)
        {
            var res = await _schedulerFactory.GetJobs();
            return Json(res);
        }
        /// <summary>
        /// 获取作业运行日志
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public IActionResult GetJobLogByPage(string taskId, int offset, int limit,string search = "")
        {
 
            var list = FileQuartz.GetJobLogByPage(taskId, offset,limit,search);
            return Json(list);
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> Add([FromBody] TaskOptions taskOptions)
        {
            return Json(await taskOptions.AddJob(_schedulerFactory, jobFactory: _jobFactory));
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        [HttpPost]
        public async Task<IActionResult> SaveJob([FromBody] TaskOptions task)
        {
            if (string.IsNullOrWhiteSpace(task.Id)) //新增
            {
                return Json(await task.AddJob(_schedulerFactory, jobFactory: _jobFactory));
            }
            else
            {//更新！
                return Json(await _schedulerFactory.Update(task));
            }
        }

        /// <summary>
        /// 切换任务状态
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> SwitchJob([FromBody] TaskOptions taskOptions)
        {
            if (taskOptions.status == (int)TriggerState.Paused)
            {
                return Json(await _schedulerFactory.Start(taskOptions));
            }
            else
            {
                return Json(await _schedulerFactory.Pause(taskOptions));
            }

        }

        [TaskAuthor]
        [HttpPost]
        public async Task<IActionResult> UpdateJob([FromBody] TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Update(taskOptions));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> Remove([FromBody] TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Remove(taskOptions));
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> BatchRemove([FromBody]List<TaskOptions> list)
        {
              await _schedulerFactory.BatchRemove(list);
      
            return Json(new Result());
        }

        [TaskAuthor]
        public async Task<IActionResult> Update([FromBody] TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Update(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Pause([FromBody] TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Pause(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Start([FromBody] TaskOptions taskOptions)
        {
         
            return Json(await _schedulerFactory.Start(taskOptions));
        }
        /// <summary>
        /// 立即执行
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> Run([FromBody] TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Run(taskOptions));
        }


    }




}